package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.statistics.TaskCostDto;
import org.jsbd.boss.domian.statistics.TaskCostVo;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.ITaskCostService;
import org.jsbd.boss.service.impl.ChannelConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/task/cost")
public class TaskCostController {

	@Resource
	private ITaskCostService taskCostService;
	@Resource
	private IChannelApkInfoService channelApkService;

	/**
	 * 任务平均市场列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listAvg")
	public String listGrid() {
		return "taskCost/listAvg";
	}

	/**
	 * 任务平均时长数据
	 * 
	 * @param page
	 *            分页
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "listAvgJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listGridJson(DataGridModel page, TaskCostVo taskCostVo) {
		String startTime = taskCostVo.getStartTime();
		String endTime = taskCostVo.getEndTime();
		if (!StringUtils.isBlank(startTime)) {
			taskCostVo.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			taskCostVo.setEndTime(endTime.replace("-", ""));
		}
		
		Map<String, Object> map = taskCostService.findListGridJson(page, taskCostVo);
		List<TaskCostDto> taskCostDtos = (List<TaskCostDto>) map.get("rows");
		List<ChannelApkConfig> apksList =  channelApkService.getAllMarkAndApkname();
		Map<String, String> apksMap = new HashMap<String, String>();
		for (ChannelApkConfig cac : apksList) {
			apksMap.put(cac.getMark(), cac.getApkName());
		}
		
		for (TaskCostDto taskCostDto : taskCostDtos) {
			taskCostDto.setApkName(apksMap.get(taskCostDto.getMark()));
		}
		return map;
	}
	
	
	/**
	 * 平均用户时长列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listAvgUser")
	public String listUser() {
		return "taskCost/listAvgUser";
	}

	/**
	 * 任务平均 用户时长数据
	 * 
	 * @param page
	 *            分页
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "listAvgUserJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listUserJson(DataGridModel page, TaskCostVo taskCostVo) {
		String startTime = taskCostVo.getStartTime();
		String endTime = taskCostVo.getEndTime();
		if (!StringUtils.isBlank(startTime)) {
			taskCostVo.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			taskCostVo.setEndTime(endTime.replace("-", ""));
		}
		
		Map<String, Object> map = taskCostService.findListUserJson(page, taskCostVo);
		List<TaskCostDto> taskCostDtos = (List<TaskCostDto>) map.get("rows");
		
		List<ChannelApkConfig> apksList =  channelApkService.getAllMarkAndApkname();
		Map<String, String> apksMap = new HashMap<String, String>();
		for (ChannelApkConfig cac : apksList) {
			apksMap.put(cac.getMark(), cac.getApkName());
		}
		
		for (TaskCostDto taskCostDto : taskCostDtos) {
			taskCostDto.setApkName(apksMap.get(taskCostDto.getMark()));
		}
		
		return map;
	}

}
